#include <stdio.h>
#include <sqlite3.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <errno.h>

#include "zlog.h"

char *err = 0;


int open_database(sqlite3 **db,int symbol)
{
    int rv;
    int result;

    if(!symbol)
    {
        rv = sqlite3_open("RPI.db",db);
        if(rv != SQLITE_OK)
        {
            dzlog_error("open database failure!\n");
            sqlite3_close(*db);
            return -1;
        }

        dzlog_info("open client_database successfully!\n");

    }
    else
    {
        rv = sqlite3_open("save.db",db);
        if(rv != SQLITE_OK)
        {
            dzlog_error("open database failure!\n");
            sqlite3_close(*db);
            return -1;
        }   

        dzlog_info("open save_database successfully!\n");
    }

    const char *sql="create table if not exists rpi(tm int,temp double,smoke int)";
    result= sqlite3_exec(*db,sql,NULL,NULL,&err);
    if(result != SQLITE_OK)
    {
        dzlog_error("open table failure!\n");
        sqlite3_close(*db);
        return -2;
    }
    dzlog_info(" table already exists!\n");

}


int get_table(sqlite3 *db)
{
	const  char *str="select * from rpi";
	int    nRow;
	int    nCol;
	char   **Result;
	int    result;

	result=sqlite3_get_table(db,str,&Result,&nRow,&nCol,&err);
	if(result != SQLITE_OK)
	{
		dzlog_error("get data from database  failure: %s\n",strerror(errno));
		sqlite3_close(db);
		return -2;
	}
	return nRow;
}

int insert_data(sqlite3 *db,time_t tm,double temp,int smoke)
{
	int te;
	te=(int)tm;
	int result;
	const char *sqli= sqlite3_mprintf("INSERT INTO rpi VALUES('%d',' %f', ' %d' );", te, temp, smoke);
	result= sqlite3_exec(db,sqli,NULL,NULL,&err);
	if(result != SQLITE_OK)
	{
		dzlog_error("insert data failure: %s\n",strerror(errno));
		sqlite3_close(db);
		return -2;
	}
	dzlog_info("insert data successfully!\n");
}

int get_data(sqlite3 *db,int *tm,double *temp,int *rowid,int *smoke)
{

	sqlite3_stmt      *pstmt = 0;
	int                rv;
	int                rv1;
	int                rv2;


	const char *arr ="select * from rpi limit 1;";
	const char *row = "select rowid from rpi limit 1;";
	rv = sqlite3_prepare_v2(db,arr,strlen(arr),&pstmt,NULL);
	if(rv != SQLITE_OK )
	{
		dzlog_error("prepare failure: %s\n",strerror(errno));
		return -6;
	}
	rv1 = sqlite3_step(pstmt);
	if(rv1 == SQLITE_DONE)
	{
		dzlog_info("get data from database successfully!\n");
	}
    
	*tm = sqlite3_column_int(pstmt,0);
	*temp = sqlite3_column_double(pstmt,1);

	*smoke = sqlite3_column_int(pstmt,2);

	rv = sqlite3_prepare_v2(db,row,strlen(arr),&pstmt,NULL);
	sqlite3_step(pstmt);
	*rowid = sqlite3_column_int(pstmt,0);

	sqlite3_finalize(pstmt);
}

int delete_data(sqlite3 *db,int rowid)
{
	int result;
	char deldata[1024];
	snprintf(deldata, sizeof(deldata),  "delete from rpi where rowid = %d;", rowid);
	result= sqlite3_exec(db,deldata,NULL,NULL,&err);
	if(result != SQLITE_OK)
	{
		dzlog_error("delete data failure: %s\n",strerror(errno));
		sqlite3_close(db);
		return -2;
	}
	else
	{
		dzlog_info("delete data from database successfully!\n");
	}




}
